package cn.gwm.flink.streaming.function.map;

import cn.gwm.flink.streaming.beans.termalRunaway.WaringTermalDwmWithStragegyPhev;
import cn.gwm.flink.streaming.constant.DefaultConstant;
import cn.hutool.json.JSONObject;
import cn.hutool.json.JSONUtil;
import org.apache.flink.api.common.functions.MapFunction;

import java.util.Objects;

/**
 * @author GW00283474
 */
public class TermalWaringNullPhevMapFunction implements MapFunction<WaringTermalDwmWithStragegyPhev, WaringTermalDwmWithStragegyPhev> {
    @Override
    public WaringTermalDwmWithStragegyPhev map(WaringTermalDwmWithStragegyPhev value) throws Exception {
        if(Objects.isNull(value.getModel())){
            value.setModel(DefaultConstant.model_name);
        }
        if(Objects.isNull(value.getLatitude())){
            value.setLatitude(DefaultConstant.latitude);
        }
        if(Objects.isNull(value.getLongitude())){
            value.setLongitude(DefaultConstant.longitude);
        }
        if(Objects.isNull(value.getBMS_BattHeatRunaway())){
            //0 没发生电池热失控  1 发生电池热失控 缺省值为0
            value.setBMS_BattHeatRunaway(DefaultConstant.BMS_BattHeatRunaway);
        }

        if(Objects.isNull(value.getBMS_BattTempMax())){
            //缺省值
            value.setBMS_BattTempMax(DefaultConstant.BMS_RMC_ModuleTemp);
        }

        if(Objects.isNull(value.getBMS_BattTempMin())){
            //缺省值
            value.setBMS_BattTempMin(DefaultConstant.BMS_RMC_ModuleTemp);
        }

        if(Objects.isNull(value.getBMS_CellMaxVolt())){
            //缺省值
            value.setBMS_CellMaxVolt(DefaultConstant.BMS_SingleCellVolt);
        }
        if(Objects.isNull(value.getBMS_CellVoltMin())){
            //缺省值
            value.setBMS_CellVoltMin(DefaultConstant.BMS_SingleCellVolt);
        }
        if(Objects.isNull(value.getBMS_BattSOC())){
            //缺省值
            value.setBMS_BattSOC(DefaultConstant.BMS_SOC);
        }

        //填充电池模组默认缺省值
        value=fillModule(value);
        //填充单体电压默认缺省值
        value=fillVolt(value);
        return value;
    }


    /**
     * 填充单体电压默认缺省值
     * @param value
     * @return
     */
    WaringTermalDwmWithStragegyPhev fillVolt(WaringTermalDwmWithStragegyPhev value){
        JSONObject obj = JSONUtil.parseObj(value);
        obj.entrySet().forEach(entry->{
            if(entry.getKey().startsWith(DefaultConstant.BMS_SINGLE_CELL_PHEV_PREFIX)){
                String key=entry.getKey();
                obj.putOpt(key,obj.getObj(key,DefaultConstant.BMS_SingleCellVolt));
            }});
        return JSONUtil.toBean(obj, WaringTermalDwmWithStragegyPhev.class);
    }
    /**
     * 填充电池模组默认缺省值
     * @param value
     * @return
     */
    WaringTermalDwmWithStragegyPhev fillModule(WaringTermalDwmWithStragegyPhev value){
        JSONObject obj = JSONUtil.parseObj(value);
        obj.entrySet().forEach(entry->{
            if(entry.getKey().startsWith(DefaultConstant.BMS_MODULE_TEMP_PHEV_PREFIX)){
                String key= entry.getKey();
                obj.putOpt(key,obj.getObj(key,DefaultConstant.BMS_BattModuleTemp));
            }
        });
        return JSONUtil.toBean(obj, WaringTermalDwmWithStragegyPhev.class);
    }


}
